package com.apptravel.web.controller.admin;

import com.apptravel.dao.AirLineCompDAO;
import com.apptravel.dao.Page;
import com.apptravel.entity.AirLineComp;
import com.apptravel.util.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import com.apptravel.util.Constants;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * ���չ�˾��̨Controller.
 * Created by IntelliJ IDEA.
 * User: Administrator
 * Date: 2012-1-13
 * Time: 12:01:04
 * To change this template use File | Settings | File Templates.
 */
@Controller
@RequestMapping("/admin/airLineComp")
public class AdminAirLineCompController extends AdminBaseController {

    private static final String EDIT = "/edit";

    private static final String ADD = "/add";

    private static final String UPDATE ="/update";

    private static final String DEL = "/del";

    private static final String HOME_VIEW = "admin/airLineComp/home";

    private static final String EDIT_VIEW = "admin/airLineComp/edit";

    private static final String REDIRECT_VIEW = "/admin/airLineComp";

    @Autowired
    private AirLineCompDAO airLineCompDao;

    /**
     * ���չ�˾
     *  列表.
     * @param model
     * @return
     */
    @RequestMapping()
    public String home(ModelMap model, String name, String code,Integer page,Integer rows) {
        Map<String, Object> params = new HashMap<String, Object>();
        if(name != null){
            params.put("name","%"+name+"%");
        }
        int pageNo = Constants.DEFAULT_PAGE;
		if(page != null && page > 0)
			pageNo = page;
		int pageSize = Constants.DEFAULT_PAGE_SIZE;
		if(rows != null && rows > 0)
			pageSize = rows;
        Page<AirLineComp> list = airLineCompDao.getList(params,pageNo,pageSize);
        model.put("page",list);

        return HOME_VIEW;
    }

    /**
     * 编辑
     *
     * @param model
     * @param id
     * @return
     */
    @RequestMapping(EDIT)
    public String edit(ModelMap model,Long id){
        if(id != null && id > 0){
           AirLineComp obj = airLineCompDao.selectById(id);
           model.put("model",obj);
        }
        return EDIT_VIEW;
    }

    /**
     * 添加
     * @param airLineComp
     * @param model
     * @return
     */
    @RequestMapping(ADD)
    public String add(AirLineComp airLineComp,ModelMap model, HttpServletResponse response){
        airLineCompDao.insert(airLineComp);
        String html = this.makeResponseHtml(0,"");
        JsonUtil.renderHtml(html,response);
        return null;
    }

    /**
     * 更新.
     * 
     * @param airLineComp
     * @param model
     * @param response
     * @return
     */
    @RequestMapping(UPDATE)
    public String update(AirLineComp airLineComp,ModelMap model,HttpServletResponse response){
        airLineCompDao.updateById(airLineComp);
        String html = this.makeResponseHtml(0,"");
        JsonUtil.renderHtml(html,response);
        return null;
    }

    /**
     * 删除.
     * @param ids
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(DEL)
    public String del(String ids, HttpServletRequest request,HttpServletResponse response){
        String[] idArray = ids.split(",");
		Map<String,Object> params = new HashMap<String,Object>();
		params.put("list", idArray);
        airLineCompDao.deleteByMap(params);
        String url = request.getContextPath()+REDIRECT_VIEW;
		String html = this.makeResponseHtml(1, url);
		JsonUtil.renderHtml(html, response);
		return null;
    }

}
